var inp1 = document.getElementsByClassName("inp1")[0];
var inp2 = document.getElementsByClassName("inp2")[0];
var btn = document.getElementsByTagName("button")[0];


function username() {
  var reg = /^[0-9a-zA-Z]{3,15}/;
  var flag = true;
  if (inp1.value === "") {
    alert("请输入用户账号");
    flag = false;
    return flag;
  } else if (reg.test(inp1.value)) {
    return flag;
  } else {
    alert("请输入正确的用户账号");
    return false;
  }
}
function paw() {
  var reg = /^[a-zA-Z]{3,9}/ig;
  var flag = true;
  if (inp2.value === "") {
    alert("请输入用户密码");
    flag = false;
    return flag;
  } else if (reg.test(inp2.value)) {
    return true;
  } else {
    alert("请输入正确的用户密码");
    flag = false;
    return flag;
  }
}


btn.onclick = function () {
  if (username() && paw()) {
    var data = {
      userAccount: inp1.value,
      userPassword: inp2.value
    }
    ajax.post("http://192.168.199.101:8080/BaorSystem/user/login", data, function (res) {
      console.log(res);
      alert("登录成功")
      sessionStorage.setItem("userAccount", inp1.value);
      localStorage.setItem("userSex",res.dataset.userInfo.userSex);
      localStorage.setItem("userPhone",res.dataset.userInfo.userPhone);
      localStorage.setItem("userAccount",res.dataset.userInfo.userAccount);
      localStorage.setItem("userPassword",res.dataset.userInfo.userPassword);
      location.href = "header.html"
    })
  }

}
